package com.huiquan.icd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.dao.IcdDiseaseSectionBlackWhiteDao;
import com.huiquan.icd.domain.IcdDiseaseSectionBlackWhite;

@Service
public class IcdDiseaseSectionBlackWhiteService extends BaseService {

	@Autowired
	private IcdDiseaseSectionBlackWhiteDao icdDiseaseSectionBlackWhiteDao;

	public ModelAndView list(Integer startStr, String keyword, String countPerPageStr, User user, String checkFlag,
			String sectionName, String flag,String exactSearchFlag) {
		boolean exactFlag = exactSearchFlag != null && exactSearchFlag.equals("1");

		Map<String, Object> param = new HashMap<>();
		param.put("checkFlag", checkFlag);
		if (StringUtils.isNotBlank(flag) && "1".equals(checkFlag)) {
			param.put("flag", flag);
		}
		if (StringUtils.isNotBlank(keyword)) {
			param.put("keyword", exactFlag ? keyword : "%" + keyword + "%");
		}
		if (StringUtils.isNotBlank(sectionName)) {
			param.put("sectionName",  exactFlag ? sectionName : "%" + sectionName + "%");
		}
		if ("0".equals(checkFlag)) {
			param.put("order", "disease_name ");
		} else {
			param.put("order", "gmt_modified desc ");
		}
		int totalSize = icdDiseaseSectionBlackWhiteDao.retrieveSize(param);
		Map<String, Integer> pageNo;
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr + "", countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr + "");
		}
		int endPage = pageNo.get("endPage");
		int start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");
		List<IcdDiseaseSectionBlackWhite> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = icdDiseaseSectionBlackWhiteDao.retrieveList(param);
		}

		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("keyword", "疾病名称");
		showSearch.put("sectionName", "科室名称");

		Map<String, String> searchMap = new HashMap<>();
		searchMap.put("keyword", keyword);
		searchMap.put("sectionName", sectionName);

		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, searchMap);
		map.put("showSearch", showSearch);
		map.put("showSearchValue", searchMap);
		map.put("check_flag", checkFlag);
		map.put("flag", flag);

		LOGGER.info("Get list end!Params:start={},wordKey={},propertyKey={},type={};total size={}",
				new Object[] { start, totalSize });
		return new ModelAndView("icd/index", map);
	}

	public ReturnData changeFlag(String id, String flag, User user) {

		icdDiseaseSectionBlackWhiteDao.updateFlag(id, flag, user.getUserId());

		return ReturnUtil.success();
	}

	public ReturnData submitPage(String id, User user) {

		icdDiseaseSectionBlackWhiteDao.updateSubmit(id.substring(0, id.length() - 1), user.getUserId());

		return ReturnUtil.success();
	}
}
